﻿
using System;
using System.IO;
using FastReport.Export;
using FastReport.Web;
using Microsoft.AspNetCore.Mvc;
using FastReport;
using FastReport.Export.Pdf;
using JYH.Business;
using JYH.Model;
using System.Web.Hosting;
using System.Data;
using Microsoft.AspNetCore.Cors;

namespace JYH.Web.MES.Controllers
{

    [Route("api/[controller]")]

    [ApiController]
    public class FRController : ControllerBase
    {

        /// <summary>
        ///物料条码打印(FastReport)
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpGet("GeneratePDF")]
        [EnableCors("AllowSpecificOrigin")]
        public IActionResult GeneratePDF([FromBody] dynamic info)
        {

            string Msg = "";
            BasBarcodeInfoBLL bll = new BasBarcodeInfoBLL();
            SysParaInfo paraInfo = new SysParaInfo();
            paraInfo = SysParaInfoBLL.GetParaInfo("MaterialBarcode", out Msg);
            string BarcodeLable = "MaterialBarcode";
            if (paraInfo != null)
            {
                BarcodeLable = paraInfo.ParaValue;
            }
            string printQty = info.printQty;
            string Ids = info.CodeID;
            string reportPath = HostingEnvironment.MapPath("~/App_Data/Lable/" + BarcodeLable + ".frx");
            DataSet dataSet = new DataSet();
            // 获取数据源
            dataSet = bll.GetWmsBarCodeInfo(Ids, printQty, out Msg);
            if (dataSet != null && dataSet.Tables[0] != null)
            {
                dataSet.Tables[0].TableName = "BasBarcodeInfo";
            }
            // 加载报表定义
            var report = new Report();
            report.Load(reportPath); // 确保路径正确
            report.Prepare();
            //report.RegisterData("/path/to/your/data.frd", DataSourceLoadMode.Load); // 如果有数据源

            // 创建PDF导出器并导出
            var pdfExport = new PDFExport();
           // pdfExport.Export(report);
            var stream = new MemoryStream();
             pdfExport.Export(report,stream);
            stream.Position = 0;
            return File(stream, "application/pdf", "report.pdf");
        }
    }
}